package com.alone.openai.api.controller;

import com.alone.openai.api.core.Api;
import com.alone.openai.api.core.param.ChatParam;
import com.alone.openai.api.core.param.ImageGenerationParam;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author Alone
 * @date 2023/3/22 10:49 上午
 */
@RestControllerAdvice
@RestController
@RequestMapping("/api")
public class ApiController {

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String handle(Exception e) {
        return e.getMessage();
    }

    @GetMapping("/chat")
    public String chat(@RequestParam String msg) {
        if (msg.startsWith("生成图片 ")) {
            String img = msg.replace("生成图片 ", "");
            return Api.IMG_V1.exec(ImageGenerationParam.of(img))
                    .getContent();
        } else {
            return Api.CHAT_V1.exec(ChatParam.of(msg))
                    .getHtmlContent();
        }
    }
}
